package com.malljelly.study.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.malljelly.study.constants.Constants;
import com.malljelly.study.dao.mapper.PaymentInfoMapper;
import com.malljelly.study.dao.po.PaymentInfo;
import com.malljelly.study.redis.BaseRedisService;
import com.malljelly.study.service.PayManager;
import com.malljelly.study.service.PayService;
import com.malljelly.study.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 骑着蜗牛去旅行 on 2018/8/28.
 */
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private BaseRedisService baseRedisService;

    @Autowired
    private AliBaBaManagerImpl aliBaBaManagerImpl;

    @Override
    public JSONObject getPayToken(PaymentInfo paymentInfo) {
        JSONObject result = new JSONObject();
        // 1.插入参数提交信息
        Integer savePaymentType = paymentInfoMapper.savePaymentType(paymentInfo);
        if (savePaymentType <= 0) {
            result.put("code", -1);
            result.put("msg", "系统错误!");
            return result;
        }
        // 2.生成对应token
        String payToken = TokenUtils.getPayToken();
        // 3.存放在redis中  key-token value -支付id
        Integer payId = paymentInfo.getId();
        baseRedisService.set(payToken, payId + "", Constants.PAY_TOKEN_MEMBER_TIME);
        // 4.返回token給消費者

        result.put("payToken", payToken);

        return result;
    }

    @Override
    public JSONObject payInfo(String payToken) {
        JSONObject result = new JSONObject();

        if (StringUtils.isEmpty(payToken)) {
            result.put("code", -1);
            result.put("msg", "token不能为空!");
            return result;
        }

        //1 从redis中通过token获取支付id
        String payId = (String) baseRedisService.get(payToken);
        if (StringUtils.isEmpty(payId)) {
            result.put("code", -1);
            result.put("msg", "支付已经超时!");
            return result;
        }

        //2.取出支付信息
        PaymentInfo paymentInfo = paymentInfoMapper.getPaymentInfo(Long.parseLong(payId));
        if (paymentInfo == null) {
            result.put("code", -1);
            result.put("msg", "未找到交易类型!");
            return result;
        }

        // 3.判断类型 调用 具体业务接口
        Long typeId = paymentInfo.getTypeId();
        PayManager payManager = null;
        // 4.调用支付接口
        if (typeId == 1) {
            payManager = aliBaBaManagerImpl;
        }
        try {
            //5.提交支付表单到阿里客户端
            String payInfo = payManager.payInfo(paymentInfo);
            result.put("code", 1);
            result.put("msg", payInfo);
            return result;
        } catch (AlipayApiException e) {
            result.put("code", -1);
            result.put("msg", "支付错误!");
            return result;
        }
    }

}
